<template >
  <div class="add_con_form tl" v-loading="loading">
    <el-dialog :close-on-click-modal="false" :title="mytitle" :modal-append-to-body="false" :visible.sync="visible" :show="show" :before-close="closeDialog" @close="$emit('update:show', false)" class="tl size_13" width="800px">
      <el-form :model="data" :rules="dataYZ" ref="data" class="mar_top" label-width="120px">
        <el-form-item label="门店图" prop="piclink">
          <el-col :span="12">
            <el-input v-model="data.piclink" size="small" clearable></el-input>
            <compressimgUpload :onSuccess="onSuccess" :pic="data.piclink" :isOneImg="true" :files="fileList"></compressimgUpload>
          </el-col>
        </el-form-item>

        <el-form-item label="店铺名称" prop="title">
          <el-col :span="12">
            <el-input v-model="data.title" clearable size="small"></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="联系人姓名" prop="name">
          <el-col :span="12">
            <el-input placeholder v-model="data.name" clearable size="small"></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="联系人手机" prop="tel">
          <el-col :span="12">
            <el-input v-model="data.tel" clearable size="small"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="营业时间" prop="business_hours">
          <el-col :span="12">
            <el-input v-model="data.business_hours" clearable size="small"></el-input>
          </el-col>
        </el-form-item>

        <!-- <el-form-item label="所属商家" prop="sid">
          <el-col :span="12">
            <el-input
              placeholder="请选择所属商家"
              v-model="data.s_username"
              disabled
              clearable
              size="small"
            ></el-input>
          </el-col>
          <el-col :span="10" :offset="1">
            <el-button
              :loading="$store.state.isLoading"
              size="small"
              type="primary"
              @click="xzshop('xzusershop')"
            >
              选择商家
            </el-button>
            <el-button
              :loading="$store.state.isLoading"
              size="small"
              type="success"
              @click="
                data.s_username = '自营';
                data.sid = 0;
              "
            >
              自营
            </el-button>
          </el-col>
        </el-form-item> -->

        <el-form-item label="管理店长" prop="uid">
          <el-col :span="12">
            <el-input placeholder="请选择管理店长" v-model="data.username" disabled clearable size="small"></el-input>
          </el-col>
          <el-col :span="5" :offset="1">
            <el-button :loading="$store.state.isLoading" size="small" type="primary" @click="xzshop('xzuser')">
              选择用户
            </el-button>
          </el-col>
        </el-form-item>

        <el-form-item label="所属地区" prop="province">
          <el-col :span="12">
            <city-cascader size="small" :placeholder="plad" :level="3" @change="handleChange" />
          </el-col>
        </el-form-item>

        <el-form-item label="地址" prop="add">
          <el-col :span="12">
            <el-input v-model="data.add" clearable size="small"></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="经纬度">
          <el-col :span="12">
            <el-input v-model="JWD" clearable size="small"></el-input>
          </el-col>
          <el-col :span="5" :offset="1">
            <a target="_blank" :href="'https://lbs.qq.com/tool/getpoint/index.html'">
              <el-button :loading="$store.state.isLoading" size="mini">
                查看经纬度
              </el-button>
            </a>
          </el-col>
        </el-form-item>

        <el-form-item>
          <el-button :loading="$store.state.isLoading" size="medium" type="primary" @click.prevent="subForm('data')">
            确定
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <keep-alive>
      <component :is="current" :show="shows" :isAll="current == 'xzusershop' ? true : false" :add_types="current == 'xzusershop' ? true : false" showType="list" @sendShop="getSend" @isshow="getShow"></component>
    </keep-alive>
  </div>
</template>


<style lang="less" scoped>
</style>

<script>
import xzuser from "@/components/currency/xz_user.vue";
import compressimgUpload from "@/components/currency/compressimgUpload.vue";
import cityCascader from "@/components/currency/cityCascader/cityCascader";
import xzusershop from "@/components/currency/xz_usershop.vue";
export default {
  components: {
    compressimgUpload,
    cityCascader,
    xzuser,
    xzusershop,
  },
  data() {
    return {
      current: "",
      shows: false,
      value1: 5,
      mytitle: "",
      fileList: [],
      updateUrl: "",
      showImg: true,
      options: [],
      selectedOptions: [], //["福建省", "福州市", "台江区"]
      JWD: "",
      optionProps: {
        value: "y",
        label: "t",
        children: "z",
      },
      value: "",
      data: {
        uid: "",
        username: "",
        sid: "",
        name: "",
        tel: "",
        title: "",
        piclink: "",
        province: "",
        city: "",
        area: "",
        town: "",
        add: "",
        longitude: "",
        latitude: "",
        s_username: "",
      },
      plad: "",
      loading: false,
      dataYZ: {
        piclink: [{ required: true, message: "请上传门店图", trigger: "blur" }],
        title: [{ required: true, message: "请输入店名", trigger: "blur" }],
        name: [
          { required: true, message: "请输入联系人姓名", trigger: "blur" },
          { min: 2, max: 6, message: "长度在 2 到 6 个字符", trigger: "blur" },
        ],
        tel: [
          { required: true, message: "请输入联系人电话", trigger: "blur" },
          { min: 11, max: 11, message: "长度在 11 个字符", trigger: "blur" },
        ],
        add: [{ required: true, message: "请输入地址", trigger: "blur" }],
        province: [
          { required: true, message: "请选择所属地区", trigger: "blur" },
        ],
      },
      visible: this.show,
    };
  },
  props: {
    show: Boolean,
    editId: String,
    clickNum: Number,
  },
  methods: {
    getShow(val) {
      this.shows = val;
    },
    getSend(row) {
      if (this.current == "xzuser") {
        this.data.username = row.username;
        this.data.uid = row.id;
      } else if (this.current == "xzusershop") {
        this.data.s_username = row.shop_title ? row.shop_title : row.username;
        this.data.sid = row.id;
      }
    },
    xzshop(str) {
      this.current = str;
      this.shows = true;
    },
    onSuccess(pic) {
      this.data.piclink = pic;
      this.showImg = true;
      this.updateUrl = this.$fnc.getImgUrl(pic);
    },
    handleChange(value) {
      if (value) {
        for (var i = 0; i < value.length; i++) {
          if (i == 0) this.data.province = value[i];
          if (i == 1) this.data.city = value[i];
          if (i == 2) this.data.area = value[i];
          if (i == 3) this.data.town = value[i];
        }
      }
    },

    closeDialog() {
      this.visible = false;
    },
    getDetails(id) {
      var data = {};
      data["id"] = id;
      this.$api.getUser.extractEdit(data).then((res) => {
        this.data = res.data.result;
        if (this.data.piclink.indexOf("http") >= 0) {
          this.updateUrl = this.data.piclink;
        } else {
          this.updateUrl = "/api/" + this.data.piclink;
        }
        this.selectedOptions = [
          this.data.province,
          this.data.city,
          this.data.area,
        ];
        if (this.data.town) {
          this.selectedOptions.push(this.data.town);
        }
        this.plad = this.selectedOptions.join("-");
        this.JWD = this.data.latitude + "," + this.data.longitude;
        if (this.data.sid == 0) {
          this.data.s_username = "自营";
        }
      });
    },
    subForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.editId == undefined) {
            this.AddNews(formName);
          } else {
            this.UpdateNews(formName);
          }
        }
      });
    },
    AddNews() {
      var JWD = this.JWD;
      var jwd_ar = JWD.split(",");
      this.data.longitude = jwd_ar[1]; //经度
      this.data.latitude = jwd_ar[0]; //纬度
      this.$api.getUser.extractSaveAdd(this.data).then((res) => {
        if (res.data.status == 1 && res.data.code == 200) {
          this.$fnc.alertSuccess({ text: "添加成功" });
          this.$emit("parent", "这里刷新父页面");
          this.visible = false;
        } else {
          this.$fnc.alertError(res.data.code_desc, res.data.result);
        }
      });
    },
    UpdateNews(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          var params = {};
          params["id"] = this.editId;
          params["name"] = this.data.name;
          params["tel"] = this.data.tel;
          params["title"] = this.data.title;
          params["province"] = this.data.province;
          params["city"] = this.data.city;
          params["area"] = this.data.area;
          params["town"] = this.data.town;
          params["piclink"] = this.data.piclink;
          params["add"] = this.data.add;
          params["uid"] = this.data.uid;
          var JWD = this.JWD;
          var jwd_ar = JWD.split(",");
          params["longitude"] = jwd_ar[1]; //经度
          params["latitude"] = jwd_ar[0]; //纬度
          params["sid"] = this.data.sid;
          params["business_hours"] = this.data.business_hours;

          this.$api.getUser.extractSaveEdit(params).then((res) => {
            if (res.data.code == 200 && res.data.result != null) {
              this.$emit("parent", "这里刷新父页面");
              this.$fnc.alertSuccess({ text: "编辑成功" }); //成功弹出框
              this.visible = false;
            } else {
              this.$fnc.alertError(res.data.result); //失败弹出框
            }
          });
        }
      });
    },
  },
  watch: {
    show() {
      this.visible = this.show;
    },
    clickNum(id) {
      if (this.editId !== undefined) {
        this.mytitle = "编辑门店";
        this.getDetails(this.editId);
      } else {
        this.mytitle = "添加门店";
        if (this.$refs["data"] !== undefined) {
          this.$refs["data"].resetFields();
        }
        this.selectedOptions = [];
        this.JWD = "";
        this.updateUrl = "";
        this.showImg = false;
      }
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.options = [];
    });
  },
  created() {},
  updated() {},
};
</script>
